// 初始化 ws 对象
var host;

$(function () {
    host = $("#hosts").val();

    client = Stomp.client('ws://' + host + ':15674/ws');
    // 定义连接成功回调函数
    var on_connect = function (x) {
        //data.body是接收到的数据
        client.subscribe("/exchange/spring-boot-exchange/spring-boot-routingKey", function (data) {
            var msg = JSON.parse(data.body);
            if (msg.type === 1) {
                var content = msg.content;
                //分类型展示
                if (content !== null && content.resiver.toString() === $("#userId").val()) {
                    $("#znxSize").text(Number($("#znxSize").text()) + 1);
                    $("#size").text(Number($("#size").text()) + 1);
                    var url = "/hy/footZnx/view?id=" + content.id;
                    var imgSrc = content.logoUrl === undefined ? "/images/logo2.png" : "/" + content.logoUrl;
                    var senderName = content.senderName;
                    var c = content.content.length > 10 ? content.content.substring(0, 10) + "..." : content.content;
                    var resiverName = content.resiverName;
                    $("#znxDetail").before("<li> <a href='" + url + "'> <img src='" + imgSrc + "' class='msg-photo' alt='' />" +
                        "<span class='msg-body'> <span class='msg-title'><span class='blue' >" + senderName + "</span><span>" + c +
                        "</span> </span> <span class='msg-time'><em class='icon-comment'></em><span>" + resiverName + "</span> </span> </span> </a> </li>");
                }
            } else if (msg.type === 2) {
                if (msg.userId.toString() === $("#userId").val()) {
                    window.location.reload();
                }
            }
        });
    };
    // 定义错误时回调函数
    var on_error = function () {
        //console.log('error');
    };

    // 连接RabbitMQ
    client.connect('admin', 'admin', on_connect, on_error, '/');
});